home *** CD-ROM | disk | FTP | other *** search
- /* Title: search.c
- * Purpose: Allow searching of STD codes or towns
- * Author: Julyan Bristow
- * Date: 23 September 1992
- * Purpose: search routine for a structure
- * Updated Feb '93 for use with doubly linked lists
- * char : *search_for - string to be searched against
- * exact: 1 for exact, 0 for approx/contains
- */
-
- #include "stdio.h"
- #include "stdlib.h"
- #include "string.h"
- #include "ctype.h"
- #include "werr.h"
- #define mtnl 50
- #define mcl 8
-
- extern struct std {
- char town[mtnl];
- char code[mcl];
- struct std *next;
- struct std *previous;
- } *stdstart,*stdlast;
-
- extern struct search {
- char town[mtnl];
- char code[mcl];
- struct search *next;
- struct search *previous;
- } *srchstart,*srchlast;
-
- extern int found;
- int s_type,leading,trailing,wildcards;
-
- /***************************** Define Search Routine *****************************/
- int string_search(char *search_for,int exact)
- {
- char *string_to_lower(char *string),*remove_wildcards(char *string);
- char find[mtnl];
- int validate(char *string,int exact);
- int exact_search(char *string,struct std *stdstart,struct search *srchstart,int s_type);
- int general_search(char *string,struct std *stdstart,struct search *srchstart,int s_type,int lead,int trail);
- struct search *srchnode;
-
- while (srchstart) { /* free any existing search lists */
- srchnode = srchstart->next;
- free(srchnode);
- srchstart = srchnode;
- }
-
- if ((srchstart = (struct search *)
- calloc(1, sizeof(struct search))) == NULL)
- { printf("Unable to assign memory for first search node.\n"); return NULL;}
-
- leading = trailing = wildcards = 0;
- if(validate(search_for,exact) == 0) return NULL;
-
- if (exact) strcpy(find,search_for);
- else {
- if (!wildcards) strcpy(find,string_to_lower(search_for));
- else strcpy(find,remove_wildcards(search_for));
- }
- /* now do the town searches */
-
- if (s_type) { /* do all the town searches */
- if (exact) return exact_search(find,stdstart,srchstart,s_type);
- else return general_search(find,stdstart,srchstart,s_type,leading,trailing);
- }
- else { /* the code searches */
- if (exact) return exact_search(find,stdstart,srchstart,s_type);
- else return general_search(find,stdstart,srchstart,s_type,leading,trailing);
- }
- return NULL;
- }
-
- int exact_search(char *string,struct std *stdstart,struct search *srchstart,int s_type)
- {
- struct std *stdnode;
- struct search *srchnode,*p = NULL;
- char match[mtnl];
- int found = 0;
- stdnode = stdstart; srchnode = srchstart;
-
- while(stdnode) {
- if (s_type) strcpy(match,stdnode->town);
- else strcpy(match,stdnode->code);
- if(!strcmp(string,match)) {
- if ((srchnode->next = (struct search *) calloc(1, sizeof(struct search))) == NULL)
- { werr(0,"Unable to assign memory for next node.\n"); return NULL;}
- strcpy(srchnode->town,stdnode->town); strcpy(srchnode->code,stdnode->code);
- srchnode->previous = p; p = srchnode; srchnode = srchnode->next;
- found++;
- }
- stdnode = stdnode->next;
- }
- if (found) {
- p->next = NULL; srchlast = p; srchstart->previous = NULL;
- return found;
- }
- return NULL;
- }
-
- int general_search(char *string,struct std *stdstart,struct search *srchstart,int s_type,
- int lead,int trail)
- {
- struct std *stdnode;
- struct search *srchnode,*p = NULL;
- char *string_to_lower(char *string),match[mtnl],new_string[mtnl];
- char *mod(char *string,int search_length);
- int len,found = 0;
- len = strlen(string);
-
- stdnode = stdstart; srchnode = srchstart;
-
- while(stdnode) {
- if (s_type) {
- if (lead || trail) {
- strcpy(new_string,string_to_lower(stdnode->town));
- strcpy(match,mod(new_string,len));
- }
- else strcpy(match,string_to_lower(stdnode->town));
- }
- else {
- if (lead || trail) {
- strcpy(new_string,string_to_lower(stdnode->code));
- strcpy(match,mod(new_string,len));
- }
- else strcpy(match,stdnode->code);
- }
- if(strstr(match,string)) {
- if ((srchnode->next = (struct search *) calloc(1, sizeof(struct search))) == NULL)
- { werr(0,"Unable to assign memory for next node.\n"); return NULL;}
- strcpy(srchnode->town,stdnode->town); strcpy(srchnode->code,stdnode->code);
- srchnode->previous = p; p = srchnode; srchnode = srchnode->next;
- found++;
- }
- stdnode = stdnode->next;
- }
- if (found) {
- p->next = NULL; srchlast = p; srchstart->previous = NULL;
- return found;
- }
- return NULL;
- }
-
-